---
title: "replication_unequal_congruence"
author: Marie Kübler
output: html_document
---


```{r, global options, warning=FALSE, include=FALSE}

# Loading R-libraries

if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, dplyr, haven, ggpubr, summarytools, kableExtra, weights, scales, patchwork, extdplyr)

knitr::opts_chunk$set(warning=FALSE, message=FALSE, echo=FALSE)

# load dataset - created with "recoding_unequal_congruence"
load(file = "unequal_congruence.Rdata")

```

# 1) Opinion differences between occupational classes

## Subsetting

```{r}
v17workers <- filter(vot2017, occup == "worker")
v17lower_employees <- filter(vot2017, occup == "lower employee")
v17higher_employees <- filter(vot2017, occup == "higher employee")
v17civil_servants <- filter(vot2017, occup == "civil servant")
v17business_owners <- filter (vot2017, occup == "business owner")
```

## Barplotting preference distributions

```{r}

bywelf <- vot2017 %>%
  filter(!is.na(occup), !is.na(welf)) %>%
  group_by(occup, welf) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

bypoleco <- vot2017 %>%
  filter(!is.na(occup), !is.na(pol_econ_fac)) %>%
  group_by(occup, pol_econ_fac) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

byinequ <- vot2017 %>%
  filter(!is.na(occup), !is.na(inequ_fac)) %>%
  group_by(occup, inequ_fac) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

byeu <- vot2017 %>%
  filter(!is.na(occup), !is.na(EU_fac)) %>%
  group_by(occup, EU_fac) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

byimmig <- vot2017 %>%
  filter(!is.na(occup), !is.na(immig)) %>%
  group_by(occup, immig) %>%
  pct_routine(occup) %>%
  mutate(pct = round(pct, 2)*100)

bylimit <- vot2017 %>%
  filter(!is.na(occup), !is.na(limit_fac)) %>%
  group_by(occup, limit_fac) %>%
  pct_routine(occup) %>%
  mutate(pct = round(pct, 2)*100)

byassim <- vot2017 %>%
  filter(!is.na(occup), !is.na(assim_fac)) %>%
  group_by(occup, assim_fac) %>%
  pct_routine(occup)%>%
  mutate(pct=round(pct, 2)*100)

byquota <- vot2017 %>%
  filter(!is.na(occup), !is.na(quota_fac)) %>%
  group_by(occup, quota_fac) %>%
  pct_routine(occup)%>%
  mutate(pct=round(pct, 2)*100)

byssmar <- vot2017 %>%
  filter(!is.na(occup), !is.na(ssmar_fac)) %>%
  group_by(occup, ssmar_fac) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

bynuc <- vot2017 %>%
  filter(!is.na(occup), !is.na(nuc_fac)) %>%
  group_by(occup, nuc_fac) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

byclim <- vot2017 %>%
  filter(!is.na(occup), !is.na(clim)) %>%
  group_by(occup, clim) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

byref <- vot2017 %>%
  filter(!is.na(occup), !is.na(ref_fac)) %>%
  group_by(occup, ref_fac) %>%
  pct_routine(occup)%>%
  mutate(pct = round(pct, 2)*100)

```

```{r}
p1 <- ggplot(data = bywelf, aes(x = occup, y = welf))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = bywelf, aes(x = occup, 
                               y = welf, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "More welfare stat/less taxes")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (vjust = 1, hjust = 1, color = "black"),
        axis.text.y=element_text(color = "black"))+
  coord_flip()

p2 <- ggplot(data = bypoleco, aes(x = occup, y = pol_econ_fac))+
  geom_point(aes(size = pct, color = occup))+
  geom_text(data = bypoleco, aes(x = occup, 
                               y = pol_econ_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "No market intervention")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p3 <- ggplot(data = byinequ, aes(x = occup, y = inequ_fac))+
  geom_point(aes(size = pct, color = occup))+
  geom_text(data = byinequ, aes(x = occup, 
                               y = inequ_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Fight income inequality")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p4 <- ggplot(data = byeu, aes(x = occup, y = EU_fac))+
  geom_point(aes(size = pct, color = occup))+
  geom_text(data = byeu, aes(x = occup, 
                               y = EU_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "EU redistribution")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p5 <- ggplot(data = byimmig, aes(x = occup, y = immig))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = byimmig, aes(x = occup, 
                               y = immig, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Facilitate/restrict immigration")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p6 <- ggplot(data = bylimit, aes(x = occup, y = limit_fac))+
  geom_point(aes(size = pct, color = occup))+
  geom_text(data = bylimit, aes(x = occup, 
                               y = limit_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Limit refugee admission")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p7 <- ggplot(data = byassim, aes(x = occup, y = assim_fac))+
  geom_point(aes(size = pct, color = occup))+
  geom_text(data = byassim, aes(x = occup, 
                               y = assim_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey ()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Assimilation of immigrants")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip() 

p8 <- ggplot(data = byquota, aes(x = occup, y = quota_fac))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = byquota, aes(x = occup, 
                               y = quota_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Gender quota")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()


p9 <- ggplot(data = byssmar, aes(x = occup, y = ssmar_fac))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = byssmar, aes(x = occup, 
                               y = ssmar_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey ()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Same sex marriage")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p10 <- ggplot(data = bynuc, aes(x = occup, y = nuc_fac))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = bynuc, aes(x = occup, 
                               y = nuc_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Use nuclear power")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p11 <- ggplot(data = byclim, aes(x = occup, y = clim))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = byclim, aes(x = occup, 
                               y = clim, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "Climate protection vs. economic growth")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

p12 <- ggplot(data = byref, aes(x = occup, y = ref_fac))+
  geom_count(aes(size= pct, color = occup))+
  geom_text(data = byref, aes(x = occup, 
                               y = ref_fac, 
                               label=pct),
            color="white", size=3)+
  scale_size_area(max_size=15)+
  scale_color_grey()+
  scale_y_discrete(labels = scales::wrap_format(10))+
  labs(x = NULL, y = NULL,
       title = "National referendums")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_text (angle = 45, vjust = 1, hjust = 1, color = "black"),
        axis.text.y = element_text (color = "black"))+
  coord_flip()

```

### Figure 1: Economic preferences per occupational class

```{r}
p3 + p2

```

### Figure 2: Cultural preferences per occupational class

```{r}
p6 + p9

```
 


# 2) Unequal congruence on the overall level

## 2.1 Barplotting distributions of MPs' preferences and preferences of citizens per class 


```{r}
barplot_5_weighted_occup <- function(variable, datar, datac, title){
ggplot (NULL, aes_string(variable)) +
  geom_bar (data=(remove_missing(datar, na.rm = TRUE, vars = variable)), aes(y=..prop.., group = 1, fill = " MPs"), alpha = 0.5)+
  geom_col (data=(remove_missing(datac, na.rm = TRUE, vars = variable)), aes(y=pct/100, fill= "citizens"), alpha = 0.5)+  
  labs (x = NULL, y = "Proportion")+
  coord_flip() +
  facet_grid (~occup)+
  ggtitle(title)+
  labs(x = NULL, y = NULL, fill=NULL)+
  scale_fill_manual(values = c("gray15", "gray78"))+
  scale_y_continuous(labels = scales::percent, limit= c(0.0, 0.75))+
  scale_x_discrete(labels = c("1" = "strongly agree", "2" = "agree", "3" = "partly agree", "4"= "disagree", "5"= "strongly disagree"))+
  theme(axis.text.x=element_text(size = 7, color="black"),
        panel.background = element_rect(fill = "white",  colour = "white",  size = 0.3, linetype = "solid"), 
        panel.grid.major = element_line(size = 0.3, linetype = 'solid', colour = "grey90"), 
        legend.text = element_text(size = 8), legend.key = element_rect(fill = "white"), legend.position="top", 
        plot.title = element_text(size = 10),
        strip.background = element_rect(color="lightgrey", fill="white", linetype="solid")) }



barplot_11_weighted_occup<- function(variable, datar, datac, title){
ggplot (NULL, aes_string(variable)) +
  geom_bar (data=(remove_missing(datar, na.rm = TRUE, vars = variable)), aes(y=..prop.., group = 1, fill = " MPs"), alpha = 0.5)+
  geom_col (data=(remove_missing(datac, na.rm = TRUE, vars = variable)), aes(y=pct/100, fill= "citizens"), alpha = 0.5)+  
  labs (x = NULL, y = "Proportion")+
  coord_flip() +
  facet_grid (~occup)+
  ggtitle(title)+
  labs(x = NULL, y = NULL, fill=NULL)+
  scale_fill_manual(values = c("gray15", "gray78"))+
  scale_y_continuous(labels = scales::percent, limit= c(0.0, 0.3))+
  scale_x_discrete(labels = scales::wrap_format(10))+
  theme(axis.text.x=element_text(size = 7, color="black"), 
        panel.background = element_rect(fill = "white",  colour = "white",  size = 0.3, linetype = "solid"), 
        panel.grid.major = element_line(size = 0.3, linetype = 'solid', colour = "grey90"), 
        legend.text = element_text(size = 8), legend.key = element_rect(fill = "white"), legend.position="top", 
        plot.title = element_text(size = 10),
        strip.background = element_rect(color="lightgrey", fill="white", linetype="solid")) }
```


```{r}
p13 <- barplot_11_weighted_occup ("welf", MP2017, bywelf, "'Welfare state'")

p14 <- barplot_5_weighted_occup ("pol_econ_fac", MP2017, bypoleco, "'no market intervention'")

p15 <- barplot_5_weighted_occup ("inequ_fac", MP2017, byinequ, "'fight income inequality'")

p16 <- barplot_5_weighted_occup ("EU_fac", MP2017, byeu, "'EU redistribution'")

p17 <- barplot_11_weighted_occup ("immig", MP2017, byimmig, "'immigration'")

p18 <- barplot_5_weighted_occup ("limit_fac", MP2017, bylimit, "'limit refugees admission'")

p19 <- barplot_5_weighted_occup ("assim_fac", MP2017, byassim, "'assimilation of immigrants'")

p20 <- barplot_5_weighted_occup ("ssmar_fac", MP2017, byssmar, "'same-sex marriage'")

p21 <- barplot_5_weighted_occup ("nuc_fac", MP2017, bynuc, "'nuclear power'")

p22 <- barplot_11_weighted_occup ("clim", MP2017, byclim, "'climate protection'")

p23 <- barplot_5_weighted_occup ("ref_fac", MP2017, byref, "'national referendums'")
```

### FIGURE 3: Opinion differences of MPs and citizens per occupational group (examples)

```{r}
p15 / p18

```


## 2.2 Calculating many-to-many congruence on the overall level

### function voters: create 1000 bootstrap samples by resampling (with replacement) from the sample data and create frequency distributions
```{r}

propfunc11 <- function(data, var){
 
  data_c <- data %>% 
  select(any_of(var)) %>% 
  filter(!is.na(var)) %>%
  rename(newvar = var)

set.seed(123)
boot_c <- as.data.frame(replicate(1000,sample(data_c$newvar,                                                 replace=TRUE)))%>% 
  mutate_all(funs(factor(., levels = c(1:11))))

prop_c <- as.data.frame(sapply(boot_c, function(x)                                 round(prop.table(table(x)),3)))

prop_c <- prop_c[order(as.numeric(row.names(prop_c))),]%>% 
  mutate(id = row_number())%>%
  pivot_longer(!id, names_to = "group", values_to = "item_v") %>%
  arrange(group)
}

propfunc5 <- function(data, var){
 
  data_c <- data %>% 
  select(any_of(var)) %>% 
  filter(!is.na(var)) %>%
  rename(newvar = var)

set.seed(123)
boot_c <- as.data.frame(replicate(1000,sample(data_c$newvar,                                                 replace=TRUE))) %>%
  mutate_all(funs(factor(., levels = c(1:5))))

prop_c <- as.data.frame(sapply(boot_c, function(x)                                 round(prop.table(table(x)),3)))

prop_c <- prop_c[order(as.numeric(row.names(prop_c))),]%>% 
  mutate(id = row_number())%>%
  pivot_longer(!id, names_to = "group", values_to = "item_v") %>%
  arrange(group)
}


```


### function congruence: measure many-to-many congruence for thousand distributions and calculate the mean level of congrunce and the standard deviation
```{r}

congfunc5 <- function(){
prop <- cbind(data1, data2)%>%
  mutate(id = row_number(),
         group = ceiling(id/5))
prop$diff <- ifelse(prop$item_mp<prop$item_v, 
                      prop$item_mp, prop$item_v)
  select(prop, group, diff) %>% 
  group_by(group) %>% 
  summarise(cong = sum(diff)) %>%
  summarise(meancong = mean(cong),
            sdcong = sd(cong, na.rm=T))
}

congfunc11 <- function(){
prop <- cbind(data1, data2)%>%
  mutate(id = row_number(),
         group = ceiling(id/11))
prop$diff <- ifelse(prop$item_mp<prop$item_v, 
                      prop$item_mp, prop$item_v)
  select(prop, group, diff) %>% 
  group_by(group) %>% 
  summarise(cong = sum(diff)) %>%
  summarise(meancong = mean(cong),
            sdcong = sd(cong, na.rm=T))
}

```

### voters 2017 and MPs 2017

#### welfare state

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, welf) %>%
  filter(!is.na(welf)) %>%
  rename(newvar = welf) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}


set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17workers, "welf"))

p1 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, welf) %>%
  filter(!is.na(welf)) %>%
  rename(newvar = welf) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}


set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17lower_employees, "welf"))

p2 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, welf) %>%
  filter(!is.na(welf)) %>%
  rename(newvar = welf) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}


set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17higher_employees, "welf"))

p3 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, welf) %>%
  filter(!is.na(welf)) %>%
  rename(newvar = welf) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}


set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17civil_servants, "welf"))

p4 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, welf) %>%
  filter(!is.na(welf)) %>%
  rename(newvar = welf) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}


set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17business_owners, "welf"))

p5 <- congfunc11()

```

```{r}
df1 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "welfare state")

```


#### politics - economy

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, pol_econ) %>%
  filter(!is.na(pol_econ)) %>%
  rename(newvar = pol_econ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1))) 

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "pol_econ"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, pol_econ) %>%
  filter(!is.na(pol_econ)) %>%
  rename(newvar = pol_econ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1))) 

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "pol_econ"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, pol_econ) %>%
  filter(!is.na(pol_econ)) %>%
  rename(newvar = pol_econ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1))) 

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "pol_econ"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, pol_econ) %>%
  filter(!is.na(pol_econ)) %>%
  rename(newvar = pol_econ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1))) 

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "pol_econ"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, pol_econ) %>%
  filter(!is.na(pol_econ)) %>%
  rename(newvar = pol_econ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1))) 

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "pol_econ"))

p5 <- congfunc5()
```

```{r}
df2 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "market intervention")
```


#### inequality

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, inequ) %>%
  filter(!is.na(inequ)) %>%
  rename(newvar = inequ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "inequ"))

p1 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, inequ) %>%
  filter(!is.na(inequ)) %>%
  rename(newvar = inequ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "inequ"))

p2 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, inequ) %>%
  filter(!is.na(inequ)) %>%
  rename(newvar = inequ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "inequ"))

p3 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, inequ) %>%
  filter(!is.na(inequ)) %>%
  rename(newvar = inequ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "inequ"))

p4 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, inequ) %>%
  filter(!is.na(inequ)) %>%
  rename(newvar = inequ) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "inequ"))

p5 <- congfunc5()

```

```{r}
df3 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "inequality")
```

#### EU

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, EU) %>%
  filter(!is.na(EU)) %>%
  rename(newvar = EU) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "EU"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, EU) %>%
  filter(!is.na(EU)) %>%
  rename(newvar = EU) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "EU"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, EU) %>%
  filter(!is.na(EU)) %>%
  rename(newvar = EU) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "EU"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, EU) %>%
  filter(!is.na(EU)) %>%
  rename(newvar = EU) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "EU"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, EU) %>%
  filter(!is.na(EU)) %>%
  rename(newvar = EU) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "EU"))

p5 <- congfunc5()
```


```{r}
df4 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "EU redistribution")
```


#### immigration

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, immig) %>%
  filter(!is.na(immig)) %>%
  rename(newvar = immig) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17workers, "immig"))

p1 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, immig) %>%
  filter(!is.na(immig)) %>%
  rename(newvar = immig) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17lower_employees, "immig"))

p2 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, immig) %>%
  filter(!is.na(immig)) %>%
  rename(newvar = immig) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17higher_employees, "immig"))

p3 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, immig) %>%
  filter(!is.na(immig)) %>%
  rename(newvar = immig) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17civil_servants, "immig"))

p4 <- congfunc11()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, immig) %>%
  filter(!is.na(immig)) %>%
  rename(newvar = immig) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17business_owners, "immig"))

p5 <- congfunc11()

```

```{r}
df5 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "immigration")
```

#### limit refugee admission

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, limit) %>%
  filter(!is.na(limit)) %>%
  rename(newvar = limit) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "limit"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, limit) %>%
  filter(!is.na(limit)) %>%
  rename(newvar = limit) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "limit"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, limit) %>%
  filter(!is.na(limit)) %>%
  rename(newvar = limit) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "limit"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, limit) %>%
  filter(!is.na(limit)) %>%
  rename(newvar = limit) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "limit"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, limit) %>%
  filter(!is.na(limit)) %>%
  rename(newvar = limit) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "limit"))

p5 <- congfunc5()
```

```{r}
df6 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "limit refugees")
```


#### assimilation immigrants

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, assim) %>%
  filter(!is.na(assim)) %>%
  rename(newvar = assim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "assim"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, assim) %>%
  filter(!is.na(assim)) %>%
  rename(newvar = assim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "assim"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, assim) %>%
  filter(!is.na(assim)) %>%
  rename(newvar = assim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "assim"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, assim) %>%
  filter(!is.na(assim)) %>%
  rename(newvar = assim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "assim"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, assim) %>%
  filter(!is.na(assim)) %>%
  rename(newvar = assim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "assim"))

p5 <- congfunc5()
```

```{r}
df7 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "assimilation immigrants")
```


#### gender quota

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, quota) %>%
  filter(!is.na(quota)) %>%
  rename(newvar = quota) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "quota"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, quota) %>%
  filter(!is.na(quota)) %>%
  rename(newvar = quota) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "quota"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, quota) %>%
  filter(!is.na(quota)) %>%
  rename(newvar = quota) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "quota"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, quota) %>%
  filter(!is.na(quota)) %>%
  rename(newvar = quota) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "quota"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, quota) %>%
  filter(!is.na(quota)) %>%
  rename(newvar = quota) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "quota"))

p5 <- congfunc5()
```

```{r}
df8 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "gender quota")
```


#### same sex marriage

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ssmar) %>%
  filter(!is.na(ssmar)) %>%
  rename(newvar = ssmar) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "ssmar"))

p1 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ssmar) %>%
  filter(!is.na(ssmar)) %>%
  rename(newvar = ssmar) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "ssmar"))

p2 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ssmar) %>%
  filter(!is.na(ssmar)) %>%
  rename(newvar = ssmar) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "ssmar"))

p3 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ssmar) %>%
  filter(!is.na(ssmar)) %>%
  rename(newvar = ssmar) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "ssmar"))

p4 <- congfunc5()

```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ssmar) %>%
  filter(!is.na(ssmar)) %>%
  rename(newvar = ssmar) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "ssmar"))

p5 <- congfunc5()

```

```{r}
df9 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "same sex marriage")
```

#### nuclear power

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, nuc) %>%
  filter(!is.na(nuc)) %>%
  rename(newvar = nuc) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "nuc"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, nuc) %>%
  filter(!is.na(nuc)) %>%
  rename(newvar = nuc) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "nuc"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, nuc) %>%
  filter(!is.na(nuc)) %>%
  rename(newvar = nuc) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "nuc"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, nuc) %>%
  filter(!is.na(nuc)) %>%
  rename(newvar = nuc) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "nuc"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, nuc) %>%
  filter(!is.na(nuc)) %>%
  rename(newvar = nuc) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "nuc"))

p5 <- congfunc5()
```

```{r}
df10 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "nuclear power")
```

#### climate protection

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, clim) %>%
  filter(!is.na(clim)) %>%
  rename(newvar = clim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17workers, "clim"))

p1 <- congfunc11()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, clim) %>%
  filter(!is.na(clim)) %>%
  rename(newvar = clim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17lower_employees, "clim"))

p2 <- congfunc11()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, clim) %>%
  filter(!is.na(clim)) %>%
  rename(newvar = clim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17higher_employees, "clim"))

p3 <- congfunc11()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, clim) %>%
  filter(!is.na(clim)) %>%
  rename(newvar = clim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17civil_servants, "clim"))

p4 <- congfunc11()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, clim) %>%
  filter(!is.na(clim)) %>%
  rename(newvar = clim) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:11)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc11(v17business_owners, "clim"))

p5 <- congfunc11()
```

```{r}
df11 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "climate protection")
```

#### referendums

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ref) %>%
  filter(!is.na(ref)) %>%
  rename(newvar = ref) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17workers, "ref"))

p1 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ref) %>%
  filter(!is.na(ref)) %>%
  rename(newvar = ref) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17lower_employees, "ref"))

p2 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ref) %>%
  filter(!is.na(ref)) %>%
  rename(newvar = ref) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17higher_employees, "ref"))

p3 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ref) %>%
  filter(!is.na(ref)) %>%
  rename(newvar = ref) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17civil_servants, "ref"))

p4 <- congfunc5()
```

```{r}
f1 <- function(i){
data_c <- MP2017 %>% 
  select(party, ref) %>%
  filter(!is.na(ref)) %>%
  rename(newvar = ref) %>% 
  sample_n(186, replace = TRUE)

data_c[data_c$party == "Left", "weights"] <- (0.0973/(sum(data_c$party== "Left")/186))
data_c[data_c$party == "SPD", "weights"] <- (0.2158/(sum(data_c$party== "SPD")/186))
data_c[data_c$party == "Greens", "weights"] <- (0.0945/(sum(data_c$party== "Greens")/186))
data_c[data_c$party == "FDP", "weights"] <- (0.1128/(sum(data_c$party== "FDP")/186))
data_c[data_c$party == "CDU-CSU", "weights"] <- (0.347/(sum(data_c$party== "CDU-CSU")/186))
data_c[data_c$party == "AfD", "weights"] <- (0.1326/(sum(data_c$party== "AfD")/186))

round(prop.table(xtabs(data_c$weights ~ factor(data_c$newvar, levels = c(1:5)))),3)}

set.seed(123)
prop.MP <- as.data.frame(t(sapply(1:1000,f1)))

data1 <- pivot_longer(prop.MP, cols = everything())%>%
  rename (item_mp = value)%>%
  select (item_mp)

data2 <- (propfunc5(v17business_owners, "ref"))

p5 <- congfunc5()
```

```{r}
df12 <- rbind(p1,p2,p3,p4,p5) %>%
    mutate(occupation = row_number(),
           occupation = occupation %>%
    factor(
      levels=1:5,
      labels=c("worker", "lower employee", "higher employee", "civil servant", "business owner")),
    item = "referendums")
```


```{r}
dfnew <- rbind(df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12) 

```

### Figure 4. Estimated Congruence between MPs and citizens per occupational group
```{r}
ggplot(data = dfnew, 
       aes(x = factor(item, level = c("referendums", "climate protection", "nuclear power", "same sex marriage", "gender quota", "assimilation immigrants", "limit refugees", "immigration",  "EU redistribution",  "inequality", "market intervention","welfare state")),
                 y = meancong,
                 ymin = meancong - sdcong,
                 ymax = meancong + sdcong,
           group = occupation,
           color = occupation))+
  geom_pointrange(position = position_dodge(width = .7))+
  labs(y = NULL, x = NULL, color=NULL)+
  scale_y_continuous(labels = scales::percent,
                     limits = c(0.4,1))+
  coord_flip()+
  theme_minimal()+
  theme(axis.text = element_text(color = "black"),
        legend.position = "top",
        legend.key.size = unit(0.3, "cm"))

ggplot(data = dfnew, 
       aes(x = factor(item, level = c("referendums", "climate protection", "nuclear power", "same sex marriage", "gender quota", "assimilation immigrants", "limit refugees", "immigration",  "EU redistribution",  "inequality", "market intervention","welfare state")),
                 y = meancong,
                 ymin = meancong - sdcong,
                 ymax = meancong + sdcong,
           group = occupation,
           color = occupation))+
  geom_pointrange(position = position_dodge(width = .7))+
  labs(y = NULL, x = NULL, color=NULL)+
  scale_color_grey()+
  scale_y_continuous(labels = scales::percent,
                     limits = c(0.4,1))+
  coord_flip()+
  theme_minimal()+
  theme(axis.text = element_text(color = "black"),
        legend.position = "top",
        legend.key.size = unit(0.3, "cm"))

```

# 3) Unequal congruence on the party level

## subsets
```{r}
cleft <- filter(MP2017, party == "Left")
cspd <- filter(MP2017, party == "SPD")
cgreens <- filter(MP2017, party == "Greens")
cfdp <- filter(MP2017, party == "FDP")
cunion <- filter(MP2017, party == "CDU-CSU")
cafd <- filter(MP2017, party == "AfD")
```

## congruence between workers and MPs per party

### functions (for MPs)

```{r}
propfunc11_mp <- function(data, var){
 
  data_mp <- data %>% 
  select(any_of(var)) %>% 
  filter(!is.na(var)) %>%
  rename(newvar = var)

set.seed(123)
boot_mp <- as.data.frame(replicate(1000,sample(data_mp$newvar, replace=TRUE)))%>% mutate_all(funs(factor(., levels = c(1:11))))
  
prop_mp <- as.data.frame(sapply(boot_mp, function(x)                                 round(prop.table(table(x)),3)))

prop_mp <- prop_mp[order(as.numeric(row.names(prop_mp))),]%>% 
  mutate(id = row_number())%>%
  pivot_longer(!id, names_to = "group", values_to = "item_mp") %>%
  arrange(group) %>%
  select(item_mp)
}

propfunc5_mp <- function(data, var){
 
  data_mp <- data %>% 
  select(any_of(var)) %>% 
  filter(!is.na(var)) %>%
  rename(newvar = var)

set.seed(123)
boot_mp <- as.data.frame(replicate(1000,sample(data_mp$newvar, replace=TRUE)))%>% mutate_all(funs(factor(., levels = c(1:5))))
  
prop_mp <- as.data.frame(sapply(boot_mp, function(x)                                 round(prop.table(table(x)),3)))

prop_mp <- prop_mp[order(as.numeric(row.names(prop_mp))),]%>% 
  mutate(id = row_number())%>%
  pivot_longer(!id, names_to = "group", values_to = "item_mp") %>%
  arrange(group) %>%
  select(item_mp)
}
```


### welfare state
```{r}
data1 <- propfunc11_mp(cleft,"welf") 
data2 <- (propfunc11(v17workers, "welf")) 
p1 <- congfunc11()

data1 <- propfunc11_mp(cspd,"welf") 
data2 <- (propfunc11(v17workers, "welf")) 
p2 <- congfunc11()
  
data1 <- propfunc11_mp(cgreens,"welf") 
data2 <- (propfunc11(v17workers, "welf")) 
p3 <- congfunc11()

data1 <- propfunc11_mp(cfdp,"welf")
data2 <- (propfunc11(v17workers, "welf")) 
p4 <- congfunc11()

data1 <- propfunc11_mp(cunion,"welf") 
data2 <- (propfunc11(v17workers, "welf"))
p5 <- congfunc11()
  
data1 <- propfunc11_mp(cafd,"welf") 
data2 <- (propfunc11(v17workers, "welf"))
p6 <- congfunc11()
    
c1 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "welfare state")

```


### politics- economy

```{r}

data1 <- propfunc5_mp(cleft,"pol_econ") 
data2 <- (propfunc5(v17workers, "pol_econ")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"pol_econ") 
data2 <- (propfunc5(v17workers, "pol_econ")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"pol_econ") 
data2 <- (propfunc5(v17workers, "pol_econ"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"pol_econ") 
data2 <- (propfunc5(v17workers, "pol_econ")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"pol_econ") 
data2 <- (propfunc5(v17workers, "pol_econ"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"pol_econ")
data2 <- (propfunc5(v17workers, "pol_econ")) 
p6 <- congfunc5()
    
c2 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "market intervention")
```

### inequality

```{r}

data1 <- propfunc5_mp(cleft,"inequ") 
data2 <- (propfunc5(v17workers, "inequ")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"inequ") 
data2 <- (propfunc5(v17workers, "inequ")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"inequ") 
data2 <- (propfunc5(v17workers, "inequ"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"inequ") 
data2 <- (propfunc5(v17workers, "inequ")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"inequ") 
data2 <- (propfunc5(v17workers, "inequ"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"inequ")
data2 <- (propfunc5(v17workers, "inequ")) 
p6 <- congfunc5()
    
c3 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "inequality")

```


### EU redistribution

```{r}

data1 <- propfunc5_mp(cleft,"EU") 
data2 <- (propfunc5(v17workers, "EU")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"EU") 
data2 <- (propfunc5(v17workers, "EU")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"EU") 
data2 <- (propfunc5(v17workers, "EU"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"EU") 
data2 <- (propfunc5(v17workers, "EU")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"EU") 
data2 <- (propfunc5(v17workers, "EU"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"EU")
data2 <- (propfunc5(v17workers, "EU")) 
p6 <- congfunc5()
    
c4 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "EU redistribution")

```


### immigration
```{r}
data1 <- propfunc11_mp(cleft,"immig") 
data2 <- (propfunc11(v17workers, "immig")) 
p1 <- congfunc11()

data1 <- propfunc11_mp(cspd,"immig") 
data2 <- (propfunc11(v17workers, "immig")) 
p2 <- congfunc11()
  
data1 <- propfunc11_mp(cgreens,"immig") 
data2 <- (propfunc11(v17workers, "immig")) 
p3 <- congfunc11()

data1 <- propfunc11_mp(cfdp,"immig")
data2 <- (propfunc11(v17workers, "immig")) 
p4 <- congfunc11()

data1 <- propfunc11_mp(cunion,"immig") 
data2 <- (propfunc11(v17workers, "immig"))
p5 <- congfunc11()
  
data1 <- propfunc11_mp(cafd,"immig") 
data2 <- (propfunc11(v17workers, "immig"))
p6 <- congfunc11()
    
c5 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "immigration")

```


### limit refugees

```{r}
data1 <- propfunc5_mp(cleft,"limit") 
data2 <- (propfunc5(v17workers, "limit")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"limit") 
data2 <- (propfunc5(v17workers, "limit")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"limit") 
data2 <- (propfunc5(v17workers, "limit"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"limit") 
data2 <- (propfunc5(v17workers, "limit")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"limit") 
data2 <- (propfunc5(v17workers, "limit"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd, "limit")
data2 <- (propfunc5(v17workers, "limit")) 
p6 <- congfunc5()
    
c6 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "limit refugees")

```


### assimilation

```{r}
data1 <- propfunc5_mp(cleft,"assim") 
data2 <- (propfunc5(v17workers, "assim")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"assim") 
data2 <- (propfunc5(v17workers, "assim")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"assim") 
data2 <- (propfunc5(v17workers, "assim"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"assim") 
data2 <- (propfunc5(v17workers, "assim")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"assim") 
data2 <- (propfunc5(v17workers, "assim"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"assim")
data2 <- (propfunc5(v17workers, "assim")) 
p6 <- congfunc5()
    
c7 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "assimilation")

```


### gender quota

```{r}
data1 <- propfunc5_mp(cleft,"quota") 
data2 <- (propfunc5(v17workers, "quota")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"quota") 
data2 <- (propfunc5(v17workers, "quota")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"quota") 
data2 <- (propfunc5(v17workers, "quota"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"quota") 
data2 <- (propfunc5(v17workers, "quota")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"quota") 
data2 <- (propfunc5(v17workers, "quota"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"quota")
data2 <- (propfunc5(v17workers, "quota")) 
p6 <- congfunc5()
    
c8 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "gender quota")

```


### same sex marriage

```{r}
data1 <- propfunc5_mp(cleft,"ssmar") 
data2 <- (propfunc5(v17workers, "ssmar")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"ssmar") 
data2 <- (propfunc5(v17workers, "ssmar")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"ssmar") 
data2 <- (propfunc5(v17workers, "ssmar"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"ssmar") 
data2 <- (propfunc5(v17workers, "ssmar")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"ssmar") 
data2 <- (propfunc5(v17workers, "ssmar"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"ssmar")
data2 <- (propfunc5(v17workers, "ssmar")) 
p6 <- congfunc5()
    
c9 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "same-sex marriage")

```


### nuclear power

```{r}
data1 <- propfunc5_mp(cleft,"nuc") 
data2 <- (propfunc5(v17workers, "nuc")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"nuc") 
data2 <- (propfunc5(v17workers, "nuc")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"nuc") 
data2 <- (propfunc5(v17workers, "nuc"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"nuc") 
data2 <- (propfunc5(v17workers, "nuc")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"nuc") 
data2 <- (propfunc5(v17workers, "nuc"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"nuc")
data2 <- (propfunc5(v17workers, "nuc")) 
p6 <- congfunc5()
    
c10 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "nuclear power")

```


### climate protection

```{r}

data1 <- propfunc11_mp(cleft,"clim") 
data2 <- (propfunc11(v17workers, "clim")) 
p1 <- congfunc11()

data1 <- propfunc11_mp(cspd,"clim") 
data2 <- (propfunc11(v17workers, "clim")) 
p2 <- congfunc11()
  
data1 <- propfunc11_mp(cgreens,"clim") 
data2 <- (propfunc11(v17workers, "clim")) 
p3 <- congfunc11()

data1 <- propfunc11_mp(cfdp,"clim")
data2 <- (propfunc11(v17workers, "clim")) 
p4 <- congfunc11()

data1 <- propfunc11_mp(cunion,"clim") 
data2 <- (propfunc11(v17workers, "clim"))
p5 <- congfunc11()
  
data1 <- propfunc11_mp(cafd,"clim") 
data2 <- (propfunc11(v17workers, "clim"))
p6 <- congfunc11()
    
c11 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "climate protection")

```


### referendums

```{r}
data1 <- propfunc5_mp(cleft,"ref") 
data2 <- (propfunc5(v17workers, "ref")) 
p1 <- congfunc5()

data1 <- propfunc5_mp(cspd,"ref") 
data2 <- (propfunc5(v17workers, "ref")) 
p2 <- congfunc5()
  
data1 <- propfunc5_mp(cgreens,"ref") 
data2 <- (propfunc5(v17workers, "ref"))
p3 <- congfunc5()

data1 <- propfunc5_mp(cfdp,"ref") 
data2 <- (propfunc5(v17workers, "ref")) 
p4 <- congfunc5()

data1 <- propfunc5_mp(cunion,"ref") 
data2 <- (propfunc5(v17workers, "ref"))
p5 <- congfunc5()
  
data1 <- propfunc5_mp(cafd,"ref")
data2 <- (propfunc5(v17workers, "ref")) 
p6 <- congfunc5()
    
c12 <- rbind(p1,p2,p3, p4, p5, p6) %>%
    mutate(occupation = "workers",
           party = row_number(),
           party = party %>%
    factor(
      levels=1:6,
      labels=c("Left", "SPD", "Greens",
               "FDP", "CDU/CSU", "AfD")),
    item = "referendums")

```

```{r}
party_congruence_workers <- rbind (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12)

```

### Figure 5: Estimated Congruence between workers and MPs per party
```{r}
ggplot(data= party_congruence_workers, 
             aes(x = party, 
                 y = meancong,
                 ymin = meancong - sdcong,
                 ymax = meancong + sdcong))+
  geom_pointrange(size = 0.3, position = position_dodge(width = .5))+
  labs(y = NULL, x = NULL, color=NULL)+
  scale_y_continuous(labels = scales::percent,
                     limits = c(0,1))+
  coord_flip()+
  theme_bw()+
  theme(axis.text = element_text(color = "black", size = 7),
        legend.position = "none")+
  facet_wrap(~factor(item, level = c("welfare state", "market intervention", "inequality", "EU redistribution", "immigration", "limit refugees", "assimilation", "gender quota", "same-sex marriage", "nuclear power", "climate protection", "referendums")), nrow = 3)

```
### Figure 6: Opinion differences on same-sex marriage between workers and MPs per party

```{r}
ssmar_by_party <- MP2017 %>%
  filter(!is.na(ssmar_fac), !is.na(ssmar)) %>%
  group_by(party, ssmar_fac) %>%
  pct_routine(party)

ggplot() +
  geom_col (data=ssmar_by_party, aes(x= ssmar_fac, y=pct, fill= "MPs"), alpha = 0.5)+
  geom_bar (data=v17workers%>%select(ssmar_fac, occup), aes(x= ssmar_fac, y=..prop.., group = 1, fill = "Workers"), alpha = 0.5)+
  coord_flip() +
  facet_grid (~party)+
  ggtitle("ssmar")+
  labs(x = NULL, y = NULL, fill=NULL)+
  scale_fill_manual(values = c("gray15", "gray78"))+
  scale_y_continuous(labels = scales::percent,limit= c(0.0, 1))+
  scale_x_discrete(limits=c("strongly agree", "agree", "in part", "disagree", "strongly disagree"))+
  theme(axis.text.x=element_text(size = 7, color="black"),
        panel.background = element_rect(fill = "white",  colour = "white",  size = 0.3, linetype = "solid"), 
        panel.grid.major = element_line(size = 0.3, linetype = 'solid', colour = "grey90"), 
        legend.text = element_text(size = 8), legend.key = element_rect(fill = "white"), legend.position="top", 
        plot.title = element_text(size = 10),
        strip.background = element_rect(color="lightgrey", fill="white", linetype="solid")) 
```
